/**
 * 游戏发射摇杆
 * by wyy
 */
class gamePad extends eui.Component implements eui.UIComponent {
    //小圈
    public target: eui.Rect;
    //大圈
    public range: eui.Rect;
    public player: player;

    public gamePadGrp: eui.Group;

    public bMove: boolean;
    /**
     * 摇杆
     * @param $player 接收摇杆信息的对象
     * @param $gamePadGrp 传入触摸信息的对象
     */
    constructor($player, $gamePadGrp) {
        super();
        this.skinName = "resource/game_skins/gamePadSkin.exml";
        this.player = $player;
        this.gamePadGrp = $gamePadGrp;

        this.anchorOffsetX = this.width / 2;
        this.anchorOffsetY = this.height / 2;

        this.visible = false;

        //点击空白部分出摇杆 
        this.gamePadGrp.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.showShootPad, this);
        this.gamePadGrp.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.move, this);
        this.gamePadGrp.addEventListener(egret.TouchEvent.TOUCH_END, this.moveEnd, this);
        this.gamePadGrp.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.moveEnd, this);

    }
    //摇杆表现和消息
    public move(evt) {
        if (!this.bMove) {
            return;
        }
        let p = new egret.Point(evt.stageX, evt.stageY);
        let center = this.parent.localToGlobal(this.x, this.y);
        let angle = Math.atan2(p.y - center.y, p.x - center.x);
        let dist = egret.Point.distance(p, center);
        if (dist < (this.range.width / 2)) {
            let p = this.globalToLocal(evt.stageX, evt.stageY);
            this.target.x = p.x;
            this.target.y = p.y;

            this.player.move(angle,dist);
        }
        else {
            this.target.x = Math.cos(angle) * (this.range.width / 2) + this.width / 2;
            this.target.y = Math.sin(angle) * (this.range.width / 2) + this.height / 2;

            this.player.move(angle,dist);
        }


    }
    //显示射击摇杆
    public showShootPad(evt) {
        this.x = evt.localX;
        this.y = evt.localY;
        this.visible = true;
        this.bMove = true;
    }
    public moveEnd(evt) {
        this.visible = false;

        this.target.x = this.width / 2;
        this.target.y = this.height / 2;

        
    }
}